<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm(),baTable.getIndex()"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >   
                <el-button class="flow-btn" type="primary" @click="toApproval" v-if="baTable.form.items!.status !=='未发起'">{{t('Approval')}}</el-button>
                <el-form
                    v-if="!baTable.form.loading"
                    class="flex"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="'120' + 'px'"
                    :rules="rules"
                    :disabled="baTable.form.operate == 'view'||baTable.form.items!.status=='待审批'"
                    >
                    <!-- :label-width="baTable.form.labelWidth + 'px'" -->
                <FormItem :label="t('xStockbuy.BIZNO')" type="string" v-model="baTable.form.items!.BIZNO" prop="BIZNO" :input-attr="{ placeholder: t('Please input field', { field: t('xStockbuy.BIZNO') }), disabled: true}" />
                <el-row>
                    <el-col :span="8">
                        <FormItem style="width:100%" :label="t('xStockbuy.FITTING')" type="string" v-model="baTable.form.items!.FITTING" prop="FITTING" :input-attr="{ placeholder: t('Please input field', { field: t('xStockbuy.FITTING') }), disabled: isApproval }" />
                    </el-col>
                    <el-col :span="8">
                        <FormItem style="width:100%" 
                        :label="t('xStockbuy.DEPT')" 
                        v-model="baTable.form.items!.DEPT" 
                        prop="DEPT" 
                        type="remoteSelect"
                        :input-attr="{
                            pk:'DEPTID',
                            field: 'DEPTNAME',
                            'remote-url':dEPARTMENT +'index',
                            placeholder: t('Click Select'),
                        }" 
                        
                        />
                    </el-col>
                    <el-col :span="8">
                        <FormItem style="width:100%" :label="t('xStockbuy.STOCKAREA')" type="string" v-model="baTable.form.items!.STOCKAREA" prop="STOCKAREA" :input-attr="{ placeholder: t('Please input field', { field: t('xStockbuy.STOCKAREA') }), disabled: isApproval }" />
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <FormItem style="width:100%" :label="t('xStockbuy.SPECS')" type="string" v-model="baTable.form.items!.SPECS" prop="SPECS" :input-attr="{ placeholder: t('Please input field', { field: t('xStockbuy.SPECS') }), disabled: isApproval }" />
                    </el-col>
                    <el-col :span="8">
                        <FormItem style="width:100%" :label="t('xStockbuy.NUMBER')" type="number" prop="NUMBER" v-model.number="baTable.form.items!.NUMBER" :input-attr="{ step: '1', placeholder: t('Please input field', { field: t('xStockbuy.NUMBER') }), disabled: isApproval }" />
                    </el-col>
                    <el-col :span="8">
                        <FormItem style="width:100%" :label="t('xStockbuy.PRICE')" type="number" prop="PRICE" v-model.number="baTable.form.items!.PRICE" :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('xStockbuy.PRICE') }), disabled: isApproval }" />
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <FormItem style="width:100%" :label="t('xStockbuy.BRAND')" type="string" v-model="baTable.form.items!.BRAND" prop="BRAND" :input-attr="{ placeholder: t('Please input field', { field: t('xStockbuy.BRAND') }), disabled: isApproval }" />
                    </el-col>
                    <el-col :span="8">
                        <FormItem style="width:100%" :label="t('xStockbuy.SQR')" 
                        v-model="baTable.form.items!.SQR" 
                        prop="SQR" 
                        type="remoteSelect"
                        :input-attr="{
                            pk: 'nickname',
                            field: 'nickname',
                            'remote-url':authAdmin+'index',
                            placeholder: t('Click Select'),
                            disabled: isApproval
                        }" 
                        />
                    </el-col>
                    <el-col :span="8">
                        <FormItem style="width:100%" :label="t('xStockbuy.OP')" type="string" v-model="baTable.form.items!.OP" prop="OP" :input-attr="{ placeholder: t('Please input field', { field: t('xStockbuy.OP') }), disabled: true }" />
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <FormItem style="width:100%" 
                        :label="t('xStockbuy.CONTAINER')" 
                        v-model="baTable.form.items!.CONTAINER" 
                        prop="CONTAINER" 
                        type="remoteSelect"
                        :input-attr="{
                            pk: 'TCNO',
                            field: 'TCNO',
                            'remote-url': '/admin/XPot/index?search[]=%7B%22field%22:%22STATUS%22,%22val%22:%22SYZ%22,%22operator%22:%22%3D%22%7D',
                        }" 
                    />
                    </el-col>
                    <el-col :span="8">
                        <FormItem style="width:100%" :label="t('xStockbuy.SQDATE')" type="datetime" v-model="baTable.form.items!.SQDATE" prop="SQDATE" :input-attr="{ placeholder: t('Please select field', { field: t('xStockbuy.SQDATE') }), disabled: isApproval }" />
                    </el-col>
                    <el-col :span="8">
                        <FormItem style="width:100%" :label="t('xStockbuy.OPTIME')" type="datetime" v-model="baTable.form.items!.OPTIME" prop="OPTIME" :input-attr="{ placeholder: t('Please select field', { field: t('xStockbuy.OPTIME') }), disabled: true }" />
                    </el-col>
                </el-row>
                <FormItem :label="t('xStockbuy.REMARK')" style="width:100%" type="textarea" v-model="baTable.form.items!.REMARK" prop="REMARK" :input-attr="{ placeholder: t('Please input field', { field: t('xStockbuy.REMARK') }), disabled: isApproval }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer v-if="baTable.form.operate !== 'view'&&baTable.form.items!.status!=='待审批'">
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="handleSave" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
                <el-button type="primary" v-if="baTable.form.items!.status == '未发起'&&!isApproval" @click="handleTj">采购申请</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject, computed } from 'vue'
import { useI18n } from 'vue-i18n'
import { useRoute } from 'vue-router'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import { ElForm, ElMessage, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { useApprovalInfo } from '/@/stores/approvalDialog'
import createAxios from '/@/utils/axios'
import { xStockbuy , dEPARTMENT ,authAdmin} from '/@/api/controllerUrls'

const approvalInfo = useApprovalInfo()
const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    FITTING: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xStockbuy.FITTING') })})],
    SPECS: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xStockbuy.SPECS') })})],
    PRICE: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xStockbuy.PRICE') })})],
    NUMBER: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xStockbuy.NUMBER') })})],
    SQDATE: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xStockbuy.SQDATE') })})],
    OPTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xStockbuy.OPTIME') })})],
})

const route = useRoute()

const isApproval = computed(()=> {
    return route.name === "xStockApproval"
})

const toApproval = ()=> {
    approvalInfo.setData({
        bizno:baTable.form.items!.BIZNO,
        type:isApproval.value?'approve':'view',
        flowname:'',
    })
    approvalInfo.setDialogShow(true)
}

const handleSave = ()=>{
    baTable.onSubmit(formRef.value,(res:any)=>{
        if(!baTable.form.items!.BIZNO){
            baTable.form.items!.BIZNO = res.data
            baTable.form.items!.status = '未发起'
        }
        baTable.toggleForm('')
        // baTable.getIndex()
        // baTable.initSort()
    })
}

const handleTj = ()=>{
    createAxios({
        url: xStockbuy + 'tj',
        params:{
            bizno:baTable.form.items!.BIZNO
        }
    }).then(()=>{
        baTable.getIndex()
        baTable.initSort()
        baTable.toggleForm('')
        ElMessage.success('提交成功！')
    })
}

</script>

<style scoped lang="scss">
.ba-operate-form{
    position: relative;
}
.flow-btn{
    position: absolute;
    top: 30px;
    left:500px;
}
</style>
